package main

import (
	"context"
	"fmt"
	"sync"
	"time"
)

func do(ctx context.Context) uint64 {
	c := uint64(0)
	for {
		select {
		case <-ctx.Done():
			return c
		default:
			c++
			time.Sleep(time.Millisecond)
		}
	}
}

func main() {
	rootContext := context.Background()
	ctx, cancelFunc := context.WithCancel(rootContext)
	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		c := do(ctx)
		fmt.Println(c)
		wg.Done()
	}()
	time.Sleep(time.Second)
	cancelFunc()
	wg.Wait()
}
